const db = require('../db/index')
const {limitCallback} = require('../utils/index')

const moment = require('moment')


//获取所有用户
exports.getUserList = (req, res) => {

    //总数
    new Promise((resolve, reject) => {
        db.query('select count(*) as total from users where is_delete=0', (err, result) => {
            if (err) return reject(err)
            resolve(result[0].total)
        })
    }).then(item => {
        return new Promise((resolve, reject) => {
            db.query('select *  from users  order by id desc limit ?,?', [limitCallback(req.query.page, req.query.limit, item), req.query.limit], (err, result) => {
                if (err) return reject(err)
                result.map(items => {
                    delete items.password
                    items.create_time = moment(item.create_time).format("YYYY-MM-DD")
                })
                resolve([result, item])
            })
        })
    }).then(result => {
        res.send({
            code: 200,
            message: "查询成功",
            data: {
                list: result[0],
                total: result[1]
            }
        })
    }).catch(error => {
        res.send({
            code: 500,
            message:"查询失败",
            data: error
        })
    })
}

//启用&&禁用用户
exports.userIsdelete = (req, res) => {
    db.query('update users set is_delete=? where id=?', [req.body.type, req.body.id], (err, result) => {
        if (err) return res.send({
            code: 500,
            message:"操作失败" ,
            data: err
        })

        if (result.affectedRows !== 1) return res.send({
            code: 500,
            message: "更新失败",
            data: {}
        })

        res.send({
            code: 200,
            message: '修改成功',
            data: {}
        })


    })
}

//管理员审核列表
exports.audit = (req, res) => {


    //总数
    new Promise((resolve, reject) => {
        db.query(`select count(r.id) as total from reimbursement as r inner join reimbursement_types as rt  inner join audit_control as ac on r.type_id=rt.id and r.id = ac.p_id and r.is_delete=0 and ac.state=${req.query.type}`, (err, result) => {
            if (err) return reject(err)
            resolve(result[0].total)
        })
    }).then(item => {
        return new Promise((resolve, reject) => {
            db.query(`select ac.id as ac_id, r.id,r.create_time,r.use_time,r.detail,r.type_id,r.price,r.images,r.is_delete,rt.type,ac.state,ac.create_time as ac_create_time,ac.images as ac_images ,ac.detail as ac_detail,ac.images as ac_images  from reimbursement as r inner join reimbursement_types as rt inner join audit_control as ac on r.type_id=rt.id and r.id = ac.p_id and r.is_delete=0 and ac.state=${req.query.type}  order by r.id desc limit ?,?`, [limitCallback(req.query.page, req.query.limit, item), req.query.limit], (err, result) => {
                if (err) return reject(err)
                result.map(items => {
                    items.create_time = moment(item.create_time).format("YYYY-MM-DD")
                    items.use_time = moment(item.create_time).format("YYYY-MM-DD")
                    if (items.images) {
                        items.images = items.images.split(",")
                    } else {
                        items.images = []
                    }

                    if (items.ac_images) {
                        items.ac_images = items.ac_images.split(",")
                    } else {
                        items.ac_images = []
                    }
                })
                resolve([result, item])
            })
        })
    }).then(result => {
        res.send({
            code: 200,
            message: "查询成功",
            data: {
                list: result[0],
                total: result[1]
            }
        })
    }).catch(error => {
        res.send({
            code: 500,
            message: "操作失败",
            data: error
        })
    })

}

//报销审核
exports.auditOperate = (req, res) => {
    db.query("select * from audit_control  where id=?", req.body.id, (err, result) => {
        if (err) return res.send({
            code: 500,
            message: "操作失败" ,
            data: err
        })

        if (result.length !== 1) return res.send({
            code: 500,
            message: '操作失败',
            data: {}
        })

        switch (req.body.type) {
            case 'bh': //驳回

                db.query("update audit_control set state=2 ,detail=? where id=?", [ req.body.ac_detail,req.body.id], (err, result) => {

                    if (err) return res.send({
                        code: 500,
                        message: "操作失败",
                        data: err
                    })

                    if (result.affectedRows !== 1) return res.send({
                        code: 500,
                        message: '操作失败',
                        data: {}
                    })

                    res.send({
                        code: 200,
                        message: '驳回成功',
                        data: {}
                    })
                })

                break;

            case 'bx'://报销

                db.query("update audit_control set state=1 ,detail=? ,images=? where id=?",[req.body.ac_detail,req.body.ac_images,req.body.id], (err, result) => {

                    if (err) return res.send({
                        code: 500,
                        message:"操作失败",
                        data: err
                    })

                    if (result.affectedRows !== 1) return res.send({
                        code: 500,
                        message: '操作失败',
                        data: {}
                    })

                    res.send({
                        code: 200,
                        message: '报销操作成功',
                        data: {}
                    })
                })

                break;
        }

    })

}


